package com.lexsoft.kanaiscube.util;

import java.lang.reflect.Field;
import java.math.BigInteger;
import java.util.HashMap;

import android.support.v4.app.Fragment;
import android.util.Log;

import com.lexsoft.kanaiscube.R;

public class Tools {
	private static HashMap<String,Class<?>> map;
	static{
		map = new HashMap<String,Class<?>>();
		map.put("string", R.string.class);
		map.put("id", R.id.class);
		map.put("drawable", R.drawable.class);
		map.put("color", R.color.class);
		map.put("anim", R.anim.class);
		map.put("array", R.array.class);
	}
	public static int getResourceIdByName(String type,String name){
		Class<?> clz = map.get(type);
		try{
			Field field = clz.getDeclaredField(name);
			return field.getInt(clz);
		}catch(Exception e){
			Log.d("lex", e.getMessage());
			return -1;
		}
	}
	public static Fragment loadFragment(String fragment){
		try{
			Class<?> cls = Class.forName(fragment);
			Object obj = cls.newInstance();
			return (Fragment)obj;
		}catch(Exception e){
			Log.d("lex", e.getMessage());
			return null;
		}
	}
	public static String toTimeString(BigInteger time){
		BigInteger[] two = time.divideAndRemainder(BigInteger.valueOf(60));
		return two[0]+":"+two[1];
	}
	public static String formatIntString(String str){
		if(str==null||"".equals(str.trim())){
			return "";
		}
		String result="";
		char[] sequences = str.trim().toCharArray();
		int mod = sequences.length%3;
		for(int i=0;i<sequences.length;i++){
			if(i%3==mod&&i!=0){
				result+=",";
			}
			result+=sequences[i];
		}
		return result;
	}
	public static double getDouble(String str,double defaultValue){
		if(str==null||"".equals(str.trim())){
			return defaultValue;
		}
		try{
			return Double.parseDouble(str);
		}catch(Exception e){
			return defaultValue;
		}
	}
}
